﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ListIt.DAL;
using SilverlightShared;
using ListItTaxi.Web;
using System.Data.Objects;
using C2dmSharp.Server;
using System.Collections.Specialized;

namespace Dispatcher.Presenter
{
    public interface ITaxiView
    {
        object TaxiDatasource { get; set; }
        void Log(string s);
    }

    public class TaxiPresenter : GenericPresenter
    {
        
        #region Private Vars
        ITaxiView _View;
        C2dmService _service;
        #endregion

        #region Private Fn
        #endregion

        public TaxiPresenter(ITaxiView view, C2dmService service) : base()
        {
            _service = service;
            _View = view;
        }

        public override void Present()
        {
            DAL.ListItEntities.Refresh(RefreshMode.StoreWins, DAL.ListItEntities.Taxis);
            var taxiView = from taxis in DAL.ListItEntities.Taxis
                               select taxis;
                           
            List<TaxiClass> taxiClassList = new List<TaxiClass>();
            List<Taxi> taxiList = taxiView.ToList();

            foreach(Taxi taxi in taxiList)
            {
                #region Do Sanity Check
                bool modified = false;

                // if driver is avaible and there is an active job on taxi, then driver is not available!
                var jobs = from activeJobs in DAL.ListItEntities.ActiveJobs
                           where activeJobs.LicenseNo == taxi.LicenseNo
                           select activeJobs;                 
                if ((taxi.Available) && (jobs.Count() > 0)) {
                    taxi.Available = false;
                    modified = true;
                }
            
                if (modified)
                    DAL.ListItEntities.SaveChanges();
                #endregion

                TaxiClass taxiClass = new TaxiClass();
                taxiClass.Name = taxi.Firstname;
                taxiClass.LicenseNo = taxi.LicenseNo;
                taxiClass.Available = taxi.Available;
                taxiClass.JobPending = taxi.JobPending;
                taxiClass.StatusChangedAt = taxi.TimeStatusChange;
                taxiClass.IPAddress = taxi.IPAddress;

                if (taxi.Longituted != null)
                    //taxiClass.PresentLocation = Google.GetLocation(Convert.ToDecimal(taxi.Longituted), 
                    //    Convert.ToDecimal(taxi.Latitude));
                    taxiClass.PresentLocation = string.Format("Lat:{0}   Long:{1}", taxi.Latitude,
                        taxi.Longituted);
                TaxiType taxiType = (TaxiType)taxi.VehicleType;
                taxiClass.VehicleType = TaxiCommon.GetVehicleType(taxiType);
                taxiClassList.Add(taxiClass);
            }

            _View.TaxiDatasource = taxiClassList;
        }

        public void SendMessage(string licenseNo, string message)
        {
            _View.Log(string.Format("Dispatching messag to driver: {0} - {1}", licenseNo, message));

            Taxi taxi = DAL.ListItEntities.Taxis.SingleOrDefault(t => t.LicenseNo == licenseNo);

            if (taxi != null)
            {
                var extras = new NameValueCollection();
                extras.Set("msg", message);

                //Queue up the message to be sent
                _service.QueueMessage(taxi.DeviceToken, extras, ProtocolConstants.C2DM_COLLAPSE_KEY_JOB);
            }
        }
    }

    public class TaxiClass
    {
        public string Name { get; set; }
        public string LicenseNo { get; set; }
        public bool Available { get; set; }
        public int? JobPending { get; set; }
        public DateTime? StatusChangedAt { get; set; }
        public string PresentLocation { get; set; }
        public string VehicleType { get; set; }
        public string IPAddress { get; set; }
    }
}
